package com.hand.wyc24548.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hand.wyc24548.entity.SsmItem;
import com.hand.wyc24548.service.SsmltemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/wuliao")
public class SsmltemController {
    @Autowired
    private SsmltemService ssmltemService;

    /**
     * 查看全部信息
     * @param model
     * @return
     */
    @RequestMapping("/chakan")
    public String selectAll(Model model){
        model.addAttribute("ssmlist",ssmltemService.selectAll());
        return "yhxx";
    }

    /**
     * 分页
     */
    @RequestMapping("/emps")						//默认显示第一页
    public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn, Model model){
        //这不是一个分页查询
        //引入PageHelper分页插件
        //在查询之前只需要调用,传入页码，以及每页的大小
        PageHelper.startPage(pn, 5);
        //startPage后面紧跟的这个查询就是一个分页查询
        List<SsmItem> emps= ssmltemService.selectAll();
        //使用pageInfo包装查询后的结果,只需要将pageInfo交给页面就行
        //封装详细的分页信息，包括有我们查询出来的数据,传入连续显示的页数
        PageInfo page = new PageInfo(emps,5);
        model.addAttribute("pageInfo",page);

        return "list";
    }

    /**
     * 根据id查看信息
     * @param itemId
     * @return
     */
    @RequestMapping("/chakanid")
    @ResponseBody
    public SsmItem selectId(long itemId){
        SsmItem ssmItem=ssmltemService.selectByPrimaryKey(itemId);
        return ssmItem;
    }

    /**
     * 添加
     * @param
     * @return
     */
    @RequestMapping(value = "/tianjia",method = RequestMethod.POST,consumes = "application/x-www-form-urlencoded;charset=UTF-8")
    public String add(HttpServletRequest request){

        String itemCode = request.getParameter("itemCode");
        String itemUom = request.getParameter("itemUom");
        String itemDescription = request.getParameter("itemDescription");
        String enabledFlag = request.getParameter("enabledFlag");
        String startActiveDate = request.getParameter("startActiveDate");
        String endActiveDate = request.getParameter("endActiveDate");

        Date date=new Date();
        Date date1=new Date();

        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = format.parse(startActiveDate);
            date1 = format1.parse(endActiveDate);

        }  catch (ParseException e) {
            e.printStackTrace();
        }

        SsmItem ssmItem=new SsmItem();
        boolean b = Boolean.parseBoolean(enabledFlag);
        ssmItem.setEnabledFlag(b);
        ssmItem.setItemCode(itemCode);
        ssmItem.setItemDescription(itemDescription);
        ssmItem.setStartActiveDate(date);
        ssmItem.setEndActiveDate(date1);
        ssmItem.setItemUom(itemUom);

        /*SsmItem ssmItem=new SsmItem();
        boolean b=Boolean.parseBoolean(ssmItem.getItemUom());

        ssmItem.setItemCode(ssmItem.getItemCode());
        ssmItem.setItemUom(ssmItem.getItemUom());
        ssmItem.setItemDescription(ssmItem.getItemDescription());
        ssmItem.setStartActiveDate(ssmItem.getStartActiveDate());
        ssmItem.setEndActiveDate(ssmItem.getEndActiveDate());
        ssmItem.setEnabledFlag(b);*/




        ssmltemService.insert(ssmItem);
        return "redirect:/wuliao/chakan";
    }

    /**
     * 修改
     * @param ssmItem
     * @return
     */
    @RequestMapping("/bianji")
    public String updateId(SsmItem ssmItem){
        ssmltemService.updateByPrimaryKeySelective(ssmItem);
        return "redirect:/wuliao/chakan";
    }

    /**
     * 删除
     * @param itemId
     * @return
     */
    @RequestMapping(value = "/shanchu")
    @ResponseBody
    public boolean delete(Long itemId){
        ssmltemService.deleteByPrimaryKey(itemId);
        return true;
    }
}
